/*
 * AbstractValueObject.java
 * version v1.01
 */

package fr.mywiki.model.jdbcAdapter.values;

import java.sql.SQLException;

import javax.ejb.ObjectNotFoundException;

import fr.mywiki.model.ejbAdapter.Value;
import fr.mywiki.model.jdbcAdapter.utils.DatabaseRecordFacade;


/**
 * This abstract class contains generic instructions that all ValueObject objects are supposed to
 * use. More precisely, it contains functions for synchronization with the base.
 * 
 * @author Thomas
 */
public abstract class AbstractValueObject extends Value {
	/**
	 * The column which corresponds to the identifier of this type.
	 */
	public static final String IDENTIFIER = "IDENTIFIER";

	/**
	 * Tells if this object is already in the database. If so, an update of the database will change
	 * its value. Otherwise it will create a new object and fills the different fields with the
	 * values inside the value object.
	 */
	private boolean isInDatabase = false;
	
	/**
	 * Removes the value from the database.
	 * 
	 * @return true if the removal has been done. If the value was not in the database, returns false.
	 */
	public void remove() {
		if(isInDatabase) {
			try {
				DatabaseRecordFacade.removeRecord(this);
			} catch (SQLException e) {
			}
			isInDatabase = false; // Now this value is not in the database anymore.
		}
	}
	
	/**
	 * Updates the value of the object in the database.
	 * 
	 * @return true if the update was correctly done.
	 */
	public void update() throws ObjectNotFoundException {
		if (isInDatabase)
			update0();
		else throw new ObjectNotFoundException("Object not in database.");
	}

	/**
	 * Effectively updates the value of the object in the database.
	 * 
	 * @return true if the update was correctly done.
	 */
	private boolean update0()  {
		try {
			DatabaseRecordFacade.updateRecord(this);
		} catch (SQLException e) {
			return false;
		}
		return true;
	}
	
//	/**
//	 * Creates a new record in the database and fills it with the attributes of this object.
//	 * @return true if the update was correctly done (SQL exception otherwise : is caught).
//	 */
//	private boolean createAndSave()  {
//		try {
//			DatabaseAdapter.createRecord(this);
//		} catch (SQLException e) {
//			return false;
//		}
//		isInDatabase = true; // Now this value is in the database
//		return true;
//	}
	
	/**
	 * Returns the name of the field name of the identifier of this data type.
	 * 
	 * @return a String object
	 */
	public static String getIdentifierField() {
		return IDENTIFIER;
	}

	public boolean isInDatabase() {
		return isInDatabase;
	}

	public void setInDatabase(boolean isInDatabase) {
		this.isInDatabase = isInDatabase;
	}
	
	/**
	 * Returns the value of the identifier of this object.
	 * 
	 * @return an Object
	 */
	public abstract Object getIdentifier();

	/**
	 * Returns the value of the field which name is given in parameter.
	 * 
	 * @param attName the name of a field of the table
	 * @return an Object
	 */
	public abstract Object get(String attName);

	/**
	 * Changes the value of an attribute of the object
	 * 
	 * @param attName the name of the attribute to change
	 * @param value the value to set
	 */
	public abstract void set(String attName, Object value);

	/**
	 * Returns the type of this value object.
	 * 
	 * @return a String object.
	 */
	public abstract String getType();

	/**
	 * Returns the name of the field which is the identifier of this type.
	 * 
	 * @return a String object
	 */
	public abstract String getIdentifierFieldName();

	/**
	 * Returns an array of String objects being the names of the different fields which constitute
	 * this type of object.
	 * 
	 * @return an array of String objects.
	 */
	public abstract String[] getFieldNames();
}
